package com.isdepci.project.eleLoad.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.isdepci.framework.web.domain.AjaxResult;

import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.eleLoad.domain.AEngineeringList;
import com.isdepci.project.eleLoad.domain.AMachineryQuota;
import com.isdepci.project.eleLoad.domain.AQuotaInformation;
import com.isdepci.project.eleLoad.mapper.AEngineeringListMapper;
import com.isdepci.project.eleLoad.mapper.AMachineryQuotaMapper;
import com.isdepci.project.eleLoad.mapper.AQuotaInformationMapper;
import com.isdepci.project.eleLoad.service.IAEngineeringListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 工程清单Service业务层处理
 *
 * @author isdepci
 * @date 2025-02-26
 */
@Service
public class AEngineeringListServiceImpl implements IAEngineeringListService {
    @Autowired
    private AEngineeringListMapper aEngineeringListMapper;
    @Autowired
    private AQuotaInformationMapper aQuotaInformationMapper;
    @Autowired
    private AMachineryQuotaMapper aMachineryQuotaMapper;

    @Override
    public AjaxResult calculateFuel(List<AEngineeringList> engineeringList) {
        JSONArray fuelConsumptions = new JSONArray();  // 用于存储每条记录的能源消耗量和原始对象
        for (AEngineeringList engineering : engineeringList) {
            String quotaCode = engineering.getQuotaCode();
            BigDecimal quantity = new BigDecimal(engineering.getQuantity());
            // 初始化系数，默认为1
            BigDecimal defaultCoefficient = BigDecimal.ONE;
            BigDecimal laborCoefficient = BigDecimal.ONE;
            BigDecimal materialCoefficient = BigDecimal.ONE;
            BigDecimal machineryCoefficient = BigDecimal.ONE;
            BigDecimal totalGasolineFor = BigDecimal.ZERO;
            BigDecimal totalDieselFor = BigDecimal.ZERO;
            BigDecimal totalEleFor = BigDecimal.ZERO;
            BigDecimal totalCoalFor = BigDecimal.ZERO;
            BigDecimal totalWoodsFor = BigDecimal.ZERO;
            BigDecimal totalWaterFor = BigDecimal.ZERO;


            // 如果定额编号中包含*，则说明有系数调节
            if (quotaCode.contains("*")) {
                String[] parts = quotaCode.split("\\*");
                if (parts.length > 1) {
                    // 提取默认系数
                    defaultCoefficient = extractCoefficient(parts[1].split(" ")[0]);
                    laborCoefficient = defaultCoefficient;
                    materialCoefficient = defaultCoefficient;
                    machineryCoefficient = defaultCoefficient;
                }
            }

            // 检查是否有特定的人工、材料、机械系数
            if (quotaCode.contains("R*")) {
                String[] rParts = quotaCode.split("R\\*");
                if (rParts.length > 1) {
                    String[] coefficientParts = rParts[1].split(" ");
                    laborCoefficient = extractCoefficient(coefficientParts[0]);
                }
            }
            if (quotaCode.contains("C*")) {
                String[] cParts = quotaCode.split("C\\*");
                if (cParts.length > 1) {
                    String[] coefficientParts = cParts[1].split(" ");
                    materialCoefficient = extractCoefficient(coefficientParts[0]);
                }
            }
            if (quotaCode.contains("J*")) {
                String[] jParts = quotaCode.split("J\\*");
                if (jParts.length > 1) {
                    String[] coefficientParts = jParts[1].split(" ");
                    machineryCoefficient = extractCoefficient(coefficientParts[0]);
                }
            }

            // 查询定额信息和机械信息
            String baseQuotaCode = quotaCode.split("\\*")[0];  // 获取不包含系数的定额编号
            List<AQuotaInformation> quotaInformations = aQuotaInformationMapper.selectAQuotaAndMachineryList(baseQuotaCode);

            for (AQuotaInformation quotaInformation : quotaInformations) {
                // 解析机械名称和型号
                String machineryFullName = quotaInformation.getIname();
                String[] nameParts = machineryFullName.split("\\s+");
                String machineryName = nameParts[0]; // 机械名称
                String model = nameParts.length > 1 ? machineryFullName.substring(machineryName.length()).trim() : "";
                BigDecimal machineryNumber = quotaInformation.getNumber();
                // 查询机械台班定额信息
                AMachineryQuota machineryQuota = aMachineryQuotaMapper.selectMachineryQuotaByName(machineryName, model);
                if (machineryQuota != null) {
                    // 计算每台机械的燃料消耗量
                    BigDecimal gasolineConsumption = machineryQuota.getGasolineConsumption().multiply(machineryNumber);
                    BigDecimal dieselConsumption = machineryQuota.getDieoConsumption().multiply(machineryNumber);
                    BigDecimal eleConsumption = machineryQuota.getEleConsumption().multiply(machineryNumber);
                    BigDecimal coalConsumption = machineryQuota.getCoalConsumption().multiply(machineryNumber);
                    BigDecimal woodsConsumption = machineryQuota.getWoodsConsumption().multiply(machineryNumber);
                    BigDecimal waterConsumption = machineryQuota.getWaterConsumption().multiply(machineryNumber);
                    // 将每种燃料的消耗累加到总数
                    totalGasolineFor = totalGasolineFor.add(gasolineConsumption);
                    totalDieselFor = totalDieselFor.add(dieselConsumption);
                    totalEleFor = totalEleFor.add(eleConsumption);
                    totalCoalFor = totalCoalFor.add(coalConsumption);
                    totalWoodsFor = totalWoodsFor.add(woodsConsumption);
                    totalWaterFor = totalWaterFor.add(waterConsumption);
                }
            }
            // 计算整个的汽油和柴油消耗量
            BigDecimal adjustedGasolineConsumption = quantity.multiply(machineryCoefficient).multiply(totalGasolineFor);
            BigDecimal adjustedDieselConsumption = quantity.multiply(machineryCoefficient).multiply(totalDieselFor);
            BigDecimal adjustedEleConsumption = quantity.multiply(machineryCoefficient).multiply(totalEleFor);
            BigDecimal adjustedCoalConsumption = quantity.multiply(machineryCoefficient).multiply(totalCoalFor);
            BigDecimal adjustedWoodsConsumption = quantity.multiply(machineryCoefficient).multiply(totalWoodsFor);
            BigDecimal adjustedWaterConsumption = quantity.multiply(machineryCoefficient).multiply(totalWaterFor);
            // 将计算结果放入engineering对象
            engineering.setGasolineConsumption(adjustedGasolineConsumption);
            engineering.setDieoConsumption(adjustedDieselConsumption);
            engineering.setEleConsumption(adjustedEleConsumption);
            engineering.setCoalConsumption(adjustedCoalConsumption);
            engineering.setWoodsConsumption(adjustedWoodsConsumption);
            engineering.setWaterConsumption(adjustedWaterConsumption);
            // 将更新后的对象添加到结果中
            fuelConsumptions.add(engineering);
        }
        return AjaxResult.success(fuelConsumptions);
    }



    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<AEngineeringList> list =  excel.getEngineeringListList();
//        list.forEach(item->{
//            if(excel.getProjectId()!=null){
//                item.setProjectId(excel.getProjectId());
//            }
//        });
        if(list.size()>0){
            aEngineeringListMapper.insertList(list);
        }
        return AjaxResult.success();
    }




    // 提取调节系数
    private static BigDecimal extractCoefficient(String coefficientPart) {
        try {
            return new BigDecimal(coefficientPart);
        } catch (NumberFormatException e) {
            return BigDecimal.ONE;
        }
    }

    /**
     * 查询工程清单
     */
    @Override
    public AEngineeringList selectAEngineeringListById(Long id) {
        return aEngineeringListMapper.selectAEngineeringListById(id);
    }

    /**
     * 查询工程清单列表
     */
    @Override
    public List<AEngineeringList> selectAEngineeringListList(AEngineeringList aEngineeringList) {
        return aEngineeringListMapper.selectAEngineeringListList(aEngineeringList);
    }

    /**
     * 新增工程清单
     */
    @Override
    public int insertAEngineeringList(AEngineeringList aEngineeringList) {
        return aEngineeringListMapper.insertAEngineeringList(aEngineeringList);
    }

    /**
     * 修改工程清单
     */
    @Override
    public int updateAEngineeringList(AEngineeringList aEngineeringList) {
        return aEngineeringListMapper.updateAEngineeringList(aEngineeringList);
    }

    /**
     * 批量删除工程清单
     */
    @Override
    public int deleteAEngineeringListByIds(Long[] ids) {
        return aEngineeringListMapper.deleteAEngineeringListByIds(ids);
    }

    /**
     * 删除工程清单信息
     */
    @Override
    public int deleteAEngineeringListById(Long id) {
        return aEngineeringListMapper.deleteAEngineeringListById(id);
    }


}
